package com.gxa.controller;

import com.gxa.common.Q;
import com.gxa.common.R;
import com.gxa.pojo.*;
import com.gxa.service.LsFinanceService;
import com.gxa.utils.DateFormatUtils;
import com.gxa.utils.DateUtils;
import com.gxa.utils.GenerateSequenceUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/12/02 10:43
 * Description:
 * Version: V1.0
 */
@Controller
@Api(value = "财务管理Test",tags = "财务管理测试接口API (李帅)")
public class LsFinanceContrpller {
    @Autowired
    private LsFinanceService lsFinanceService;
    @RequestMapping(value = "/queryFinanceList" ,method = RequestMethod.GET)
    @ApiOperation(value ="收支列表查询" ,notes = "所有收支状况查询" , httpMethod ="get",response = Q.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage" ,value = "当前页数",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "currentLimit",value = "当前页面显示行数",paramType ="query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "操作成功"),
            @ApiResponse(code = 404,message = "找不到页面"),
            @ApiResponse(code = 500,message = "后台服务错误"),
    })
    @ResponseBody
    public Q queryFinanceList(@RequestParam(name ="currentPage") Integer currentPage, @RequestParam(name = "currentLimit")Integer currentLimit){
        System.out.println(currentPage+"===================="+currentLimit);
        return lsFinanceService.queryFinanceList(currentPage,currentLimit);
    }
    @RequestMapping(value = "/queryTodayFinanceList" ,method = RequestMethod.GET)
    @ApiOperation(value ="今日收支情况查询" ,notes = "今日收支情况查询" , httpMethod ="get",response = Q.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage" ,value = "当前页数",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "currentLimit",value = "当前页面显示行数",paramType ="query",dataType = "String",required = true)
    })

    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
public Q queryTodayFinanceList(@RequestParam(name = "currentPage" ) Integer currentPage, @RequestParam(name = "currentLimit")Integer currentLimit){
        return lsFinanceService.queryTodayFinanceList(currentPage,currentLimit);
}

    @RequestMapping(value = "/queryWeekFinanceList" ,method = RequestMethod.GET)
    @ApiOperation(value ="本周收支情况查询" ,notes = "本周收支情况查询" , httpMethod ="get",response = Q.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage" ,value = "当前页数",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "currentLimit",value = "当前页面显示行数",paramType ="query",dataType = "String",required = true)
    })

    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public Q queryWeekFinanceList(@RequestParam(name = "currentPage" ) Integer currentPage, @RequestParam(name = "currentLimit")Integer currentLimit){
        return lsFinanceService.queryWeekFinanceList(currentPage,currentLimit);
    }
    @RequestMapping(value = "/queryMonthFinanceList" ,method = RequestMethod.GET)
    @ApiOperation(value ="本月收支情况查询" ,notes = "本月收支情况查询" , httpMethod ="get",response = Q.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage" ,value = "当前页数",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "currentLimit",value = "当前页面显示行数",paramType ="query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public Q queryMonthFinanceList(@RequestParam(name = "currentPage" ) Integer currentPage, @RequestParam(name = "currentLimit")Integer currentLimit){
        return lsFinanceService.queryMonthFinanceList(currentPage,currentLimit);
    }

    @RequestMapping(value = "/queryExportFinanceList" ,method = RequestMethod.POST)
    @ApiOperation(value ="高级搜索查询页面" ,notes = "高级搜索查询页面" , httpMethod ="post",response = Q.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public Q queryExportFinanceList(@RequestBody @ApiParam (name = "QueryExportFinance",value ="数据导出栏搜索条件",required = true)QueryExportFinance queryExportFinance){
        return lsFinanceService.queryExportFinanceList(queryExportFinance);
    }

    @RequestMapping(value = "/querySearchFinance" ,method = RequestMethod.GET)
    @ApiOperation(value ="普通搜索接口" ,notes = "普通搜索接口" , httpMethod ="get",response = Q.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage" ,value = "当前页数",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "currentLimit",value = "当前页面显示行数",paramType ="query",dataType = "String",required = true),
            @ApiImplicitParam(name = "search",value = "搜索条件",paramType ="query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public Q querySearchFinance(@RequestParam(name = "currentPage" ) Integer currentPage, @RequestParam(name = "currentLimit")Integer currentLimit, @RequestParam(name = "search") String search){
        return lsFinanceService.querySearchFinance(currentPage,currentLimit,search);
    }
    @RequestMapping(value = "/ExportFinanceList" ,method = RequestMethod.POST)
    @ApiOperation(value ="数据导出" ,notes = "数据导出" , httpMethod ="post",response = Q.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public Q ExportFinanceList(@RequestBody @ApiParam (name = "exportFinance",value ="数据导出栏搜索条件",required = true)ExportFinance exportFinance){
        Integer currentPage=1;
        Integer currentLimit=5;
        return queryFinanceList(currentPage,currentLimit);
    }
    @RequestMapping(value = "/addFinance" ,method = RequestMethod.POST)
    @ApiOperation(value ="新增收支接口" ,notes = "新增收支接口" , httpMethod ="post",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R addFinance(@RequestBody @ApiParam (name = "Finance",value ="财务信息实体",required = true)Finance finance) {
        return lsFinanceService.addFinance(finance);
    }
    @RequestMapping(value = "/addFinanceUpload" ,method = RequestMethod.POST)
    @ApiOperation(value ="上传附件接口" ,notes = "上传附件接口" , httpMethod ="post",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R addFinanceUpload(@ApiParam (name = "FinanceUpload",value ="财务信息实体",required = true)FinanceUpload financeUpload, HttpServletRequest request) throws IOException {
        R r = new R();
        if(financeUpload.getFinance_EnclosureUpload().isEmpty()){
            r.setCode(400);
            r.setMsg("没有选择到文件");
            return r;
        }else {
            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if(!file.exists()){
                file.mkdirs();
            }
            MultipartFile multipartFile = financeUpload.getFinance_EnclosureUpload();
            String Fname=multipartFile.getOriginalFilename();
            String newNo= GenerateSequenceUtil.generateSequenceNo();
            String fileName =newNo+Fname.substring(Fname.lastIndexOf("."));
             File newFile = new File(uploadPath + fileName);
            //获取绝对路线
            r.setData(String.valueOf(newFile));
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(),newFile);
            r.setCode(200);
            r.setMsg("操作成功");
            return r;
        }
    }

    @RequestMapping(value = "/updateStateFinance" ,method = RequestMethod.POST)
    @ApiOperation(value ="审核、删除状态修改接口" ,notes = "审核、删除状态修改接口" , httpMethod ="get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "finance_No" ,value = "主键，编码",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "finance_State",value = "状态(0:待审核,1:已审核)",paramType ="query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R updateStateFinance(@RequestParam(name = "finance_No" ) Integer finance_No, @RequestParam(name = "finance_State")Integer finance_State){
        return lsFinanceService.UpdateStateFinance(finance_No,finance_State);
    }
    @RequestMapping(value = "/updateDeleteFinance" ,method = RequestMethod.GET)
    @ApiOperation(value ="删除财务信息接口" ,notes = "删除财务信息接口" , httpMethod ="get",response = R.class)
    @ApiImplicitParam(name = "finance_No" ,value = "主键，编码",paramType = "query",dataType = "String",required = true)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R updateDeleteFinance(@RequestParam(name = "finance_No" )Integer finance_No){
        return lsFinanceService.updateDeleteFinance(finance_No);
    }
    @RequestMapping(value = "/updateDeleteCustomerFinance" ,method = RequestMethod.GET)
    @ApiOperation(value ="根据客户编号删除财务信息接口" ,notes = "根据客户编号删除财务信息接口" , httpMethod ="get",response = R.class)
    @ApiImplicitParam(name = "finance_customer" ,value = "主键，编码",paramType = "query",dataType = "String",required = true)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R updateDeleteCustomerFinance(@RequestParam(name = "finance_customer" )Integer finance_customer){
        return lsFinanceService.updateDeleteCustomerFinance(finance_customer);
    }
    @RequestMapping(value = "/updateFinance" ,method = RequestMethod.POST)
    @ApiOperation(value ="修改财务信息接口" ,notes = "修改财务信息接口" , httpMethod ="post",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R updateFinance(@RequestBody @ApiParam (name = "Finance",value ="修改财务信息",required = true)Finance finance){
        System.out.println(finance.toString()+"接受结果");
        return lsFinanceService.UpdateFinance(finance);
    }
    @RequestMapping(value = "/queryAddFinance" ,method = RequestMethod.GET)
    @ApiOperation(value ="收支新增界面导入基本信息接口" ,notes = "收支新增界面导入基本信息接口" , httpMethod ="get",response = R.class)
    @ApiImplicitParam(name = "rae_Type_No" ,value = "收支编号",paramType = "query",dataType = "String",required = true)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    public R queryAddFinance(@RequestParam(name = "rae_Type_No" ) Integer rae_Type_No){
        return lsFinanceService.queryAddFinance(rae_Type_No);
    }
    @RequestMapping(value = "/AddFinanceOrders" ,method = RequestMethod.POST)
    @ApiOperation(value ="订单新增假接口" ,notes = "订单新增假接口" , httpMethod ="post",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public R AddFinanceOrders(@RequestBody @ApiParam (name = "Orders",value ="订单信息",required = true) FOrders orders){
        R r =lsFinanceService.addOrders(orders);
        lsFinanceService.addFinanceOrders(orders);
        return r;
    }
    @RequestMapping(value = "/AddFinanceContractL" ,method = RequestMethod.POST)
    @ApiOperation(value ="合同新增假接口" ,notes = "合同新增假接口" , httpMethod ="post",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public R AddFinanceContractL(@RequestBody @ApiParam (name = "FContract",value ="合同信息",required = true) FContract contract){
        R r=lsFinanceService.addContract(contract);
        lsFinanceService.addFinanceContract(contract);
        return r;
    }
    @RequestMapping(value = "/AddFinancePurchase" ,method = RequestMethod.POST)
    @ApiOperation(value ="采购新增假接口" ,notes = "采购新增假接口" , httpMethod ="post",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "操作成功"),
            @ApiResponse(code = 404 , message = "找不到页面"),
            @ApiResponse(code = 500 , message = "后台服务错误"),
    })
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public R AddFinancePurchase(@RequestBody @ApiParam (name = "FPurchase",value ="采购信息",required = true) FPurchase purchase){
        R r = lsFinanceService.addPurchase(purchase);
        lsFinanceService.addFinancePurchase(purchase);
        return r;
    }


}
